const app = getApp();
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
const {
  $Toast
} = require('../../components/base/base');

Page({

  data: {
    navBg: app.globalData.navBg,
    navColor: app.globalData.navColor,
    windowHeight: app.globalData.windowHeight - app.globalData.navHeight,
    navTop: app.globalData.navHeight,
    back: true,
    navBox: true,
    home: true,
    isShowPassword: true,
    newPwd: '',
    passWord: '',
    passWordClose: false,
    newPwdClose: false,
    disabled: false, //按钮是否禁用
    detain: true
  },


  //按钮禁用
  detainButton: function() {
    let {
      newPwd,
      passWord
    } = this.data
    if (newPwd && passWord) {
      this.setData({
        detain: false
      })
    } else {
      this.setData({
        detain: true
      })
    }

  },

  //监听新密码
  NewPwdInput: function(e) {
    var newPwd = e.detail.value
    this.setData({
      newPwd: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (newPwd == null || newPwd == undefined || newPwd.length == 0 || newPwd == '') {
      this.setData({
        newPwdClose: false
      })
    } else {
      this.setData({
        newPwdClose: true
      })
    }
    this.detainButton()
  },

  NewPwdFocus: function(e) {
    var newPwd = e.detail.value
    this.setData({
      newPwd: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (newPwd == null || newPwd == undefined || newPwd.length == 0 || newPwd == '') {
      this.setData({
        newPwdClose: false
      })
    } else {
      this.setData({
        newPwdClose: true
      })
    }
  },

  NewPwdBlur: function(e) {
    this.setData({
      newPwdClose: false
    })
  },

  CloseNPInput: function(e) {
    var newPwd = e.detail.value
    this.setData({
      newPwd: '',
      newPwdClose: false
    });
    this.detainButton()
  },

  //监听确认密码
  PassWordInput: function(e) {
    var passWord = e.detail.value
    this.setData({
      passWord: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (passWord == null || passWord == undefined || passWord.length == 0 || passWord == '') {
      this.setData({
        passWordClose: false
      })
    } else {
      this.setData({
        passWordClose: true
      })
    }
    this.detainButton()
  },

  PassWordFocus: function(e) {
    var passWord = e.detail.value
    this.setData({
      passWord: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (passWord == null || passWord == undefined || passWord.length == 0 || passWord == '') {
      this.setData({
        passWordClose: false
      })
    } else {
      this.setData({
        passWordClose: true
      })
    }
  },

  PassWordBlur: function(e) {
    this.setData({
      passWordClose: false
    })
  },

  ClosePWInput: function(e) {
    var passWord = e.detail.value
    this.setData({
      passWord: '',
      passWordClose: false
    });
    this.detainButton()
  },

  //修改密码
  exitPwd: util.throttle(function(e) {

    var that = this;
    var newPwd = that.data.newPwd;
    var passWord = that.data.passWord;
    if (newPwd == "" || newPwd == undefined || newPwd == null || newPwd.length == 0) {
      $Toast({
        content: '请输入新密码!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (newPwd.length < 6 || newPwd.length > 18) {
      $Toast({
        content: '密码长度在6~18位之间,请重新输入!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (passWord == "" || passWord == undefined || passWord == null || passWord.length == 0) {
      $Toast({
        content: '请输入确认密码!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (newPwd !== passWord) {
      $Toast({
        content: '两次密码输入不相同!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else {
      wx.request({
        url: util.api + api.ModifyCusInfo,
        data: {
          BUSINESS_PARAMETERS: {
            Mobile: '',
            smscode: '',
            newMobile: '',
            newPassword: newPwd
          },
          SYSTEM_PARAMETERS: {
            APP_ID: "M_P_G001",
            METHOD_ID: 0,
            ACCESS_TOKEN: "1w2Re3bE^og38",
            TIMESTAMP: util.formatTime(new Date(), 'Y/M/D h:m:s'),
            SIGN: "string"
          }
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          if (res.data.MESSAGE.MESSAGE_CODE == 1) {
            $Toast({
              content: '修改成功!',
              icon: 'success',
              duration: 1
            });
      
            wx.navigateBack({
              delta: 1
            })
          }
  
        },
        fail: function (res) {}
      })
      
    }

  }, 1000),

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }

})